package net.zhengxinyun.performance.controller.exam;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.data.SelectOptionBean;
import net.zhengxinyun.performance.dto.ExamResultDto;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.ExamQuestionFilter;
import net.zhengxinyun.performance.service.exam.ExamResult.ExamResultService;
import net.zhengxinyun.performance.service.exam.exam.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.OpMinus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/examManage")
public class ExamResultCtrl  {

    @Autowired
    private ExamResultService resultService;
    @Autowired
    private ExamService examService;

    @PostMapping("/singleExamResult")
    public Result<HashMap<String,Object>> getSingleExamResult(@RequestBody ExamResultParam param)
    {

        HashMap<String,Object> map = new HashMap<>();
        List<Map<String,Object>>  exlist = new ArrayList<>();
        List<Map<String,Object>>  tableExList = new ArrayList<>();
      //  param.filter = new ExamResultDto();
      //  param.filter.setExamId(new Long(58));
      //  param.filter.setExamer("6");

        ExamQuestionFilter qf = new ExamQuestionFilter();
       qf.setExamId(param.filter.getExamId());
       qf.setExamer(param.filter.getExamer());
        //所有题干
        List<ExamQuestionFilter> examList = examService.selectExam(qf);
      //  List<ExamResultDto> singExamResult = resultService.getSingExamResult(param.filter);
        //遍历题干 获取选项值
        for (ExamQuestionFilter item : examList) {

            HashMap<String,Object> mapexam = new HashMap<>();
            mapexam.put("examId",item.getExamId());
            mapexam.put("questionId",item.getId());
            mapexam.put("stem",item.getSort()+"、"+item.getStem());
            mapexam.put("optionId",item.getOptionId());
            mapexam.put("selectId",item.getAnswer());
            mapexam.put("A",null);
            mapexam.put("B",null);
            mapexam.put("C",null);
            mapexam.put("D",null);
            mapexam.put("E",null);
            mapexam.put("F",null);


            param.filter.setQuestionId(item.getId());
            List<ExamResultDto> singExamResult = resultService.getSingExamResult(param.filter);
            List<SelectOptionBean> beans = Util.setSelectList(item);
            //封装表格视图数据
            for (ExamResultDto dto : singExamResult) {
                mapexam.put(dto.getAnswer(),dto.getNums());
            }
            tableExList.add(mapexam);
            // 封装题干视图数据
            for (SelectOptionBean bean : beans) {
                {


                    if("A".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> A = singExamResult.stream().filter(dto -> dto.getAnswer().equals("A")).collect(Collectors.toList());
                        try {
                           // opmap.put("ACount",A.get(0).getNums());
                            bean.setCount(A.get(0).getNums());
                        }catch (IndexOutOfBoundsException e)
                        {
                            bean.setCount(0);
                            //opmap.put("ACount",0);
                        }
                    }


                    if("B".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> B = singExamResult.stream().filter(dto -> dto.getAnswer().equals("B")).collect(Collectors.toList());
                        try {
                            bean.setCount(B.get(0).getNums());
                        }catch (IndexOutOfBoundsException e){
                            bean.setCount(0);
                        }
                    }


                    if("C".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> C = singExamResult.stream().filter(dto -> dto.getAnswer().equals("C")).collect(Collectors.toList());
                        try {
                            bean.setCount(C.get(0).getNums());
                        }catch (IndexOutOfBoundsException e){
                            bean.setCount(0);
                        }
                    }


                    if("D".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> D = singExamResult.stream().filter(dto -> dto.getAnswer().equals("D")).collect(Collectors.toList());
                        try {
                            bean.setCount(D.get(0).getNums());
                        }catch (IndexOutOfBoundsException e){
                            bean.setCount(0);
                        }
                    }

                    if("E".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> E = singExamResult.stream().filter(dto -> dto.getAnswer().equals("E")).collect(Collectors.toList());
                        try {
                            bean.setCount(E.get(0).getNums());
                        }catch (IndexOutOfBoundsException e){
                            bean.setCount(0);
                        }
                    }



                    if("F".equals(bean.getSelectId()))
                    {
                        List<ExamResultDto> F = singExamResult.stream().filter(dto -> dto.getAnswer().equals("F")).collect(Collectors.toList());
                        try {
                            bean.setCount(F.get(0).getNums());
                        }catch (IndexOutOfBoundsException e){
                            bean.setCount(0);
                        }
                    }





                }
            }

            if(singExamResult.size()!=0)

            mapexam.put("selectList", beans);
            exlist.add(mapexam);

        }

        map.put("list",exlist);
        map.put("tableList",tableExList);
        return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功",map);

    }


    @PostMapping("/singleExamResultTable")
    public Result<HashMap<String,Object>> getSingleExamResultTable(@RequestBody ExamResultParam param)
    {

        HashMap<String,Object> map = new HashMap<>();
        List<Map<String,Object>>  exlist = new ArrayList<>();

        ExamQuestionFilter qf = new ExamQuestionFilter();
        qf.setExamId(param.filter.getExamId());
        qf.setExamer(param.filter.getExamer());
        List<ExamQuestionFilter> examList = examService.selectExam(qf);
        for (ExamQuestionFilter item : examList) {

            HashMap<String,Object> mapexam = new HashMap<>();
            mapexam.put("examId",item.getExamId());
            mapexam.put("questionId",item.getId());
            mapexam.put("stem",item.getStem());
            mapexam.put("optionId",item.getOptionId());
            mapexam.put("selectId",item.getAnswer());
            mapexam.put("A",null);
            mapexam.put("B",null);
            mapexam.put("C",null);
            mapexam.put("D",null);
            mapexam.put("E",null);
            mapexam.put("F",null);
            param.filter.setQuestionId(item.getId());
            //这道题目的所有的答案
            List<ExamResultDto> singExamResult = resultService.getSingExamResult(param.filter);
            for (ExamResultDto dto : singExamResult) {
               mapexam.put(dto.getAnswer(),dto.getNums());
            }
            exlist.add(mapexam);

        }

        map.put("list",exlist);
        return ResultUtils.getResults(Result.Status.SUCCESS,"操作成功",map);

    }



}

class ExamResultParam{
    public ExamResultDto filter;
    public TokenEntity tokenEntity;
}